#!/usr/bin/python
##LazyFile - File Manager for lazy people
##Scans a folder for specific files, then moves those files into the specified directory
#Copyright (C) 2008 Laseredd1
#This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA



import os
import re
import shutil
import sys

class LazyFile:
    def __init__(self, Dir):
        self.fList = os.listdir(Dir)
        self.fNum = len(self.fList) - 1
        self.toMove = []
        self.While = 0
        
    def process(self, Ftype, Where):
        self.Where = Where
        
        while self.While <= self.fNum:
            self.srch = bool(re.search(Ftype, self.fList[self.While]))
            if self.srch:
                if self.fList[self.While] == "lazyfile.py" or self.fList[self.While] == "lazyfilerun.py" or self.fList[self.While] == "lazyconfig.py" or self.fList[self.While] == "lazyfile.conf": 
                    print "Cannot move required file:", self.fList[self.While]
                else:
                    self.toMove.append(self.fList[self.While])
            else: pass
            self.While += 1
            
        self.toMovNum = len(self.toMove) - 1
        self.While = 0
        self.do()
        
    def do(self):
        while self.While <= self.toMovNum:
            self.toMove2 = self.toMove[self.While]
            
            try: 
                shutil.copyfile(self.toMove2, self.Where + self.toMove2)
                os.remove(self.toMove[self.While])
            except:
                print "Error in moving:", self.toMove2, "to", self.Where
                
            self.While += 1

        print "Task completed"



if __name__ == "__main__":
    try: 
        sys.argv[1]
    except:
        print "Please run Lazyfile using lazyfilerun.py"
        raw_input("Press Enter to continue.")
    
    params = file(sys.argv[1], "r")
    paramsLst = params.readlines(0)
    
    directory = paramsLst[0].replace("\n", "")
    
    filetype = paramsLst[1].replace("\n", "")
    destination = paramsLst[2].replace("\n", "")
    
    print "Running: ", sys.argv[1]
    print "Searching For:", filetype
    print "Moving to:", destination
    print
    
    x = LazyFile(directory)
    x.process(filetype, destination)
